<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { getBannerListService, addBannerService, updateBannerService, deleteBannerService } from '@/apis/banner'


const banners = ref([])
const total = ref(0)
const params = ref({
    page: 1,
    pageSize: 5
})

const getBannerList = async () => {
    const res = await getBannerListService(params.value)
    banners.value = res.data.items
    total.value = res.data.total
}
onMounted(() => getBannerList())

// 页数切换
const pageChange = (page) => {
    params.value.page = page
    getBannerList()
}

//添加轮播图数据模型
const banner = ref({
    imgUrl: ''
})

//控制添加分类弹窗
const dialogVisible = ref(false)
//控制标题展示
const title = ref('')

// 上传成功的回调函数
const uploadSuccess = (result) => {
    banner.value.imgUrl = result.data
    console.log(result.data);
}

const addBanner = async () => {
    await addBannerService(banner.value)
    ElMessage.success("添加成功")
    getBannerList()
    dialogVisible.value = false
}

//展示编辑弹窗
const showDialog = (row) => {
    dialogVisible.value = true
    title.value = '编辑轮播图'
    //数据拷贝
    banner.value.imgUrl = row.imgUrl
    banner.value.id = row.id
}

const updateBanner= async () => {
    await updateBannerService(banner.value)
    ElMessage.success("修改成功")
    getBannerList()
    dialogVisible.value = false
}
const deleteBanner = (row) => {
    ElMessageBox.confirm(
        '你确认删除该轮播图信息吗？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //用户点击了确认
            await deleteBannerService(row.id)
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            getBannerList()
        })
        .catch(() => {
            //用户点击了取消
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

const clearData = () => {
    banner.value.imgUrl = ""
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>轮播图管理</span>
                <div class="extra">
                    <el-button type="primary"
                        @click="dialogVisible = true; title = '添加轮播图'; clearData()">添加轮播图</el-button>
                </div>
            </div>
        </template>
        <el-table :data="banners" style="width: 100%">
            <el-table-column label="id" prop="id" width="70"> </el-table-column>
            <el-table-column label="轮播图图片" prop="imgUrl">
                <template v-slot="scope">
                    <img :src="scope.row.imgUrl" :alt="'图片' + scope.$index" style="width: 300px; height: 100px;">
                </template>
            </el-table-column>
            <el-table-column label="创建时间" prop="createTime" ></el-table-column>
            <el-table-column label="更新时间" prop="updateTime" ></el-table-column>


            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteBanner(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination :total="total" @current-change="pageChange" :page-size="params.pageSize" background
                layout="prev, pager, next" />
        </div>

        <!-- 添加轮播图弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="banner" label-width="100px" style="padding-right: 30px">
                <el-form-item label="轮播图">
                    <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false" action="/api/upload"
                        name="file" :on-success="uploadSuccess">
                        <img v-if="banner.imgUrl" :src="banner.imgUrl" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">+</el-icon>
                    </el-upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title == '添加轮播图' ? addBanner() : updateBanner()"> 确认
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}

.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}
</style>